﻿using EM.FrameWork.Paging;
using EM.OA.BLL;
using EM.OA.Common;
using EM.OA.DataModel.DBModel;
using EM.OA.Portal.Models;
using EM.OA.WebCommon;
using EM.OA.WebCommon.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EM.OA.Portal.Controllers
{
    public class RemindController : BaseAuthController
    {
        /// <summary>
        /// 返回Index视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string objectID)
        {
            if (!string.IsNullOrEmpty(GetParam("ObjectID")))
            {
                ViewData["RemindObjectID"] = Guid.Parse(GetParam("ObjectID"));
            }
            else
            {
                ViewData["RemindObjectID"] = Guid.Empty.ToString();
            }
            if (!string.IsNullOrEmpty(GetParam("RemindUserID")))
            {
                ViewData["RemindUserID"] = Guid.Parse(GetParam("RemindUserID"));
            }
            else
            {
                ViewData["RemindUserID"] = Guid.Empty.ToString();
            }
            if (!string.IsNullOrEmpty(GetParam("RemindRoleID")))
            {
                ViewData["RemindRoleID"] = Guid.Parse(GetParam("RemindRoleID"));
            }
            else
            {
                ViewData["RemindRoleID"] = Guid.Empty.ToString();
            }
            if (!string.IsNullOrEmpty(GetParam("RemindDepartmentID")))
            {
                ViewData["RemindDepartmentID"] = Guid.Parse(GetParam("RemindDepartmentID"));
            }
            else
            {
                ViewData["RemindDepartmentID"] = Guid.Empty.ToString();
            }
            //ViewData["RemindID"] = "";
            //RemindBLL bll = new RemindBLL();
            //List<EMOA_Remind> list = bll.GetList(p => p.ObjectID == Guid.Parse(objectID));
            //if (list.Count <= 0)
            return View();
            //else
            //{
            //    RemindModels model = new RemindModels();
            //    model = (RemindModels)model.ToModel<EMOA_Remind>(list[0]);
            //    list.ForEach(p =>
            //    {
            //        if (p.RemindMethod == (int)GlobalEnum.RemindKind.Email)
            //            model.Mail = "on";
            //        else if (p.RemindMethod == (int)GlobalEnum.RemindKind.SMS)
            //            model.SMS = "on";
            //        else if (p.RemindMethod == (int)GlobalEnum.RemindKind.OAMessage)
            //            model.WebMessage = "on";
            //    });
            //ViewData["RemindID"] = "";
            //list.ForEach(p => ViewData["RemindID"] += p.RemindID + ",");
            //    return View();
            //}
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="rows">每页条数</param>
        /// <returns></returns>
        public JsonResult GetList(string page, string rows)
        {
            List<object> result = new List<object>();
            RemindBLL bll = new RemindBLL();
            PagedList<EMOA_Remind> list = bll.GetPagedList(Convert.ToInt32(page), Convert.ToInt32(rows));
            list.ForEach(p =>
            {
                result.Add(new RemindModels().ToModel(p));
            });

            return ToJson(result, list.TotalItemCount);
        }

        public List<EMOA_Remind> CollectList(RemindModels model)
        {
            List<EMOA_Remind> entitys = new List<EMOA_Remind>();
            if (model.Mail.IsNotEmpty())
                entitys.Add(new EMOA_Remind { RemindMethod = (int)GlobalEnum.RemindKind.Email });
            if (model.SMS.IsNotEmpty())
                entitys.Add(new EMOA_Remind { RemindMethod = (int)GlobalEnum.RemindKind.SMS });
            if (model.WebMessage.IsNotEmpty())
                entitys.Add(new EMOA_Remind { RemindMethod = (int)GlobalEnum.RemindKind.OAMessage });

            //即时提醒
            if (model.Instant.IsNotEmpty())
            {
                #region 即时提醒
                entitys.ForEach(p =>
                {
                    p.RemindType = (int)GlobalEnum.RemindTimeKind.Now;
                    p.RemindID = Guid.NewGuid();
                    p.ObjectID = Guid.Parse(model.ObjectID);
                    p.RemindUserID = Guid.Empty;
                    p.RemindRoleID = Guid.Empty;
                    p.RemindDepartmentID = Guid.Empty;
                    //p.RepeatTime = "";
                    //p.RemindTime = DateTime.MinValue;
                    //p.BeginTime = DateTime.MinValue;
                    //p.EndTime = DateTime.MinValue;
                    //p.RemindedLastDate = DateTime.MinValue;
                    p.RemindState = 0;
                    p.CreateBy = CurrentUser.UserID;
                    p.CreateTime = DateTime.Now;
                });
                #endregion
            }
            else if (model.Single.IsNotEmpty())//单次提醒
            {
                #region 单次提醒
                entitys.ForEach(p =>
                {
                    p.RemindType = (int)GlobalEnum.RemindTimeKind.Once;
                    p.RemindID = Guid.NewGuid();
                    p.ObjectID = Guid.Parse(model.ObjectID);
                    p.RemindUserID = Guid.Empty;
                    p.RemindRoleID = Guid.Empty;
                    p.RemindDepartmentID = Guid.Empty;
                    //p.RepeatTime = "";
                    p.RemindTime = model.singleRemindTime;
                    //p.BeginTime = DateTime.MinValue;
                    //p.EndTime = DateTime.MinValue;
                    //p.RemindedLastDate = DateTime.MinValue;
                    p.RemindState = 0;
                    p.CreateBy = CurrentUser.UserID;
                    p.CreateTime = DateTime.Now;
                });
                #endregion
            }
            else if (model.Multi.IsNotEmpty())//重复提醒
            {
                #region 重复提醒
                entitys.ForEach(p =>
                {
                    p.RemindType = model.repeatType.ToInt();
                    p.RemindID = Guid.NewGuid();
                    p.ObjectID = Guid.Parse(model.ObjectID);
                    p.RemindUserID = Guid.Empty;
                    p.RemindRoleID = Guid.Empty;
                    p.RemindDepartmentID = Guid.Empty;
                    if (p.RemindType == (int)GlobalEnum.RemindTimeKind.Month)
                        p.RepeatTime = model.month;
                    else if (p.RemindType == (int)GlobalEnum.RemindTimeKind.Year)
                        p.RepeatTime = model.monthDay;
                    else if (p.RemindType == (int)GlobalEnum.RemindTimeKind.Week)
                    {
                        if (model.week1.IsNotEmpty())
                            p.RepeatTime = "1,";
                        if (model.week2.IsNotEmpty())
                            p.RepeatTime += "2,";
                        if (model.week3.IsNotEmpty())
                            p.RepeatTime += "3,";
                        if (model.week4.IsNotEmpty())
                            p.RepeatTime += "4,";
                        if (model.week5.IsNotEmpty())
                            p.RepeatTime += "5,";
                        if (model.week6.IsNotEmpty())
                            p.RepeatTime += "6,";
                        if (model.week7.IsNotEmpty())
                            p.RepeatTime += "7,";
                        p.RepeatTime.TrimEnd(';');
                    }

                    p.RemindTime = model.multiRemindTime;
                    p.BeginTime = model.startDate;
                    p.EndTime = model.endDate;
                    //p.RemindedLastDate = DateTime.MinValue;
                    p.RemindState = 0;
                    p.CreateBy = CurrentUser.UserID;
                    p.CreateTime = DateTime.Now;
                });
                #endregion
            }
            entitys.ForEach(p =>
            {
                if (!string.IsNullOrEmpty(GetParam("RemindUserID")))
                    p.RemindUserID = Guid.Parse(GetParam("RemindUserID"));
                if (!string.IsNullOrEmpty(GetParam("RemindRoleID")))
                    p.RemindRoleID = Guid.Parse(GetParam("RemindRoleID"));
                if (!string.IsNullOrEmpty(GetParam("RemindDepartmentID")))
                    p.RemindDepartmentID = Guid.Parse(GetParam("RemindDepartmentID"));
            });
            return entitys;
        }

        /// <summary>
        /// 增加实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(RemindModels model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    List<EMOA_Remind> entitys = CollectList(model);

                    RemindBLL bll = new RemindBLL();
                    bool result = false;
                    if (!GetParam("edit").IsNullOrEmpty())
                        bll.Delete(p => p.ObjectID == Guid.Parse(model.ObjectID));

                    result = bll.Add(entitys);
                    if (result)
                    {
                        ViewData["RemindObjectID"] = model.ObjectID.ToString();
                        return View();
                    }
                    else
                    {
                        return Content("增加失败");
                    }
                }
                else
                    return Content("验证失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(RemindModels model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    RemindBLL bll = new RemindBLL();
                    List<EMOA_Remind> entitys = CollectList(model);
                    if (bll.Update(entitys))
                        return Content("OK");
                    else
                        return Content("修改失败");
                }
                else
                {
                    return Content("验证失败");
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string id)
        {
            try
            {
                RemindBLL bll = new RemindBLL();
                if (bll.Delete(Guid.Parse(id)))
                    return Content("OK");
                else
                    return Content("删除失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

    }
}